// pages/my_rights/index.js
const app     = getApp()
const request = require('../../utils/request.js');
const config = require('../../utils/config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nowList:{  total: 0,
            current_page: 0,
            last_page: 1,
            data: []
        },
        showPop1:false,
        service:[] ,
        choose_id:0, 
        card_id:0,  
    },
    togglePop1(){
        if(this.data.showPop1){
            this.setData({
                service:[],
                choose_id:0
            })
        }
      this.setData({
        showPop1:!this.data.showPop1
      })
    },
    showPop(e){
       this.setData({
           card_id:e.currentTarget.dataset.id
       })
       var service=this.data.nowList.data[e.currentTarget.dataset.index]['services'];
     
       if(!service||service.length==0){
        wx.showToast({
          title: '不可预约',
          icon:'none'
        })
        return false;
       }
       this.setData({
        service:service,
        showPop1:true
       })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
         this.init_Data()
    },
    init_Data(){
        var that = this;
        that.setData({
          'nowList.current_page': 0,
      });
      that.getList();
    
    
      },
      choose_tags(e){
        this.setData({
            choose_id:e.currentTarget.dataset.id
        })
      },
      goappointh(){
        var json={},that =this;
        json.card_id=that.data.card_id;
        var card_id =that.data.card_id;
        var tags_id=that.data.choose_id;
        if(tags_id==0){
          wx.showToast({
            title: '请选择服务类型',
            icon:'none'
          })
          return false
        }
        request.requestLoading(
         "/api/mine/checkInfo",
         json,
         'POST',
       ).then((res) => {
        
        if(res.code==1){
            that.setData({
                showPop1:false
            })
            wx.navigateTo({
                url: '/pages/choose_hospital/index?card_id='+card_id+'&tags_id='+tags_id,
              })   
        //  wx.navigateTo({
        //      url: '/pages/make_appointment/index?card_id='+that.data.card_id+'&hosptial_id='+e.currentTarget.dataset.id,
        //    })
        }
        
        
   
       }).catch((err) => {
         console.error(err);
       });
      
      },
      godetail(e){
      wx.navigateTo({
        url: '/pages/card_detail/index?id='+e.currentTarget.dataset.id,
      })
      },
      getList()
      {
        var that = this;
        var json = {};
        json.nindex=that.data.nindex;
        json.keywords=that.data.keywords;
        json.page = Number(that.data.nowList.current_page) + 1;
        if (json.page > 1 && that.data.nowList.current_page >= that.data.nowList.last_page) {       
            return false;
        }
        request.requestLoading(
            "/api/appoint/rightsList",
            json,
            'POST',
          ).then((res) => {
            var nowList = res.data;
            that.setData({
                is_show:true
            })
    
            if (json.page > 1) {
                var now_nowList_data =that.data.nowList.data || [];
                var now_nowList_data = now_nowList_data.concat(nowList.data);
                nowList.data = now_nowList_data;
            }
      
            that.setData({
              nowList: nowList
            });
          
           
           
      
          }).catch((err) => {
            console.error(err);
          });
      },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.init_Data()
        setTimeout(function(){
          wx.stopPullDownRefresh();
        },500)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        var that =this;

        if (that.data.nowList.current_page >= that.data.nowList.last_page) {
          // functions.toast('已加载到底部了');
          return false;
      } else {
        that.getList();
      }
    },

   
})